﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DataModel.Models;

namespace DataModel.Managers
{
    public class SpecialityManager
    {
        UniversityPortalEntities entityModel;

        public SpecialityManager()
        {
            entityModel = new UniversityPortalEntities();
        }

        public void CreateSpeciality(string shortName, string fullName, string departamentShortName, string description)
        {
            CreateSpeciality(shortName, fullName, (new DepartmentManager()).GetDepartamenGuid(departamentShortName), description);
        }

        public void CreateSpeciality(string shortName, string fullName, Guid departamentID, string description)
        {
            if (entityModel.Specialities.Any(sp => sp.shortName == shortName))
            {
                throw new Exception(string.Format("Speciality \"{0}\" already exists", shortName));
            }

            var spec = new Speciality();
            spec.ID = Guid.NewGuid();
            spec.shortName = shortName;
            spec.fullName = fullName;
            spec.description = description;
            spec.depatmentID = departamentID;

            entityModel.AddToSpecialities(spec);
            entityModel.SaveChanges();
        }


        public Guid GetSpecialityGuid(string shortName)
        {
            Speciality speciality;
            try
            {
                speciality = entityModel.Specialities.First(sp => sp.shortName == shortName);
            }
            catch
            {
                throw new Exception(string.Format("Speciality with name \"{0}\" doesn't exist", shortName));
            }

            return speciality.ID;
        }


        //public Speciality GetSpeciality(Guid specialityID)
        //{
        //    Speciality speciality;
        //    try
        //    {
        //        speciality = entityModel.Specialities.First(dep => dep.ID == specialityID);
        //    }
        //    catch
        //    {
        //        throw new Exception(string.Format("Speciality with ID \"{0}\" doesn't exist", specialityID.ToString()));
        //    }

        //    return speciality;
        //}

        public Speciality GetSpeciality(string shortName)
        {
            Speciality Speciality;
            try
            {
                Speciality = entityModel.Specialities.First(dep => dep.shortName == shortName);
            }
            catch
            {
                throw new Exception(string.Format("Speciality with name \"{0}\" doesn't exist", shortName));
            }

            return Speciality;
        }

        //public Speciality GetSpeciality(string fullName)
        //{
        //    Speciality Speciality;
        //    try
        //    {
        //        Speciality = entityModel.Specialitys.First(dep => dep.fullName == fullName);
        //    }
        //    catch
        //    {
        //        throw new Exception(string.Format("Speciality with name \"{0}\" doesn't exist", fullName));
        //    }

        //    return Speciality;
        //}

        public Speciality GetSpeciality(string shortName, string fullName)
        {
            Speciality Speciality;
            try
            {
                Speciality = entityModel.Specialities.First(dep => (dep.shortName == shortName) && (dep.fullName == fullName));
            }
            catch
            {
                throw new Exception(string.Format("Speciality with name \"{0} - {1}\" doesn't exist", shortName, fullName));
            }

            return Speciality;
        }

        public SpecialityModel GetSpeciality(Speciality speciality)
        {
            var specialityModel = new SpecialityModel();
            specialityModel.DepartmentShortName = speciality.Department.shortName;
            specialityModel.Description = speciality.description;
            specialityModel.ShortName = speciality.shortName;
            specialityModel.FullName = speciality.fullName;
            specialityModel.SpecialityID = speciality.ID;
            var groupManager = new GroupManager();
            foreach (var group in speciality.Groups)
            {
                specialityModel.Groups.Add(groupManager.GetGroupModel(group));
            }
            return specialityModel;
        }

        public SpecialityModel GetSpeciality(Guid specialityID)
        {
            Speciality sp = entityModel.Specialities.FirstOrDefault(s => s.ID == specialityID);
            if (sp != null)
            {
                return GetSpeciality(sp);
            }
            return null;
        }


        public void DeleteSpeciality(Guid specialityID)
        {
            Speciality speciality;
            try
            {
                speciality = entityModel.Specialities.First(dep => dep.ID == specialityID);
            }
            catch
            {
                throw new Exception(string.Format("Speciality with ID \"{0}\" doesn't exist", specialityID.ToString()));
            }

            entityModel.Specialities.DeleteObject(speciality);
            entityModel.SaveChanges();
        }

        public void DeleteSpeciality(string shortName)
        {
            Speciality Speciality;
            try
            {
                Speciality = entityModel.Specialities.First(dep => dep.shortName == shortName);
            }
            catch
            {
                throw new Exception(string.Format("Speciality with name \"{0}\" doesn't exist", shortName));
            }

            entityModel.Specialities.DeleteObject(Speciality);
            entityModel.SaveChanges();
        }

        //public void DeleteSpeciality(string fullName)
        //{
        //    Speciality Speciality;
        //    try
        //    {
        //        Speciality = entityModel.Specialitys.First(dep => dep.fullName == fullName);
        //    }
        //    catch
        //    {
        //        throw new Exception(string.Format("Speciality with name \"{0}\" doesn't exist", fullName));
        //    }

        //    entityModel.Specialitys.DeleteObject(Speciality);
        //    entityModel.SaveChanges();
        //}

        public void DeleteSpeciality(string shortName, string fullName)
        {
            Speciality Speciality;
            try
            {
                Speciality = entityModel.Specialities.First(dep => (dep.shortName == shortName) && (dep.fullName == fullName));
            }
            catch
            {
                throw new Exception(string.Format("Speciality with name \"{0} - {1}\" doesn't exist", shortName, fullName));
            }

            entityModel.Specialities.DeleteObject(Speciality);
            entityModel.SaveChanges();
        }

        public SpecialityModel GetSpeciality(GroupModel group)
        {
            return null;
        }

        public IQueryable<StudentModel> GetStudents(GroupModel group)
        {
            return null;
        }

        public JournalModel GetJournal(GroupModel group)
        {
            return null;
        }

    }
}
